import {ref} from "vue";
import * as XLSX from "xlsx";

const exportHeader = ref([[]])
const exportKey = ref({})
const processSelect = (rows, columns: any[]) => {
    exportHeader.value = [[]]
    exportKey.value = {}
    columns.map((item: any, i: number) => {
        if (item.visible!=false) {
            if (item.label != undefined && item.label != "操作" && item.prop != undefined) {
                exportHeader.value[0].push(item.label)
                exportKey.value[item.prop] = 1
            }
        }
    });
    for (let i = 0; i < rows.length; i++) {
        let content = []  //序号
        let data = rows[i];
        for (const key in exportKey.value) {
            content.push(data[key])
        }
        exportHeader.value.push(content)
    }
}

export const exportToXlsx = (rows, columns, fileName) => {
    processSelect(rows, columns);
    let WorkSheet = XLSX.utils.aoa_to_sheet(exportHeader.value);
    let new_workbook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(new_workbook, WorkSheet, 'sheet');
    XLSX.writeFile(new_workbook, `${fileName}.xlsx`);
};

